import axios from './axios.js'
import qs from 'qs'

const headers = {
  'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
}
const userUrl = '/user20012'
const goodsUrl = '/goods20013'
const payUrl = '/payment20014'
// 获取用户已完成的订单
const getOrderFinished = async (data) => {
  const rst = await axios.get(payUrl + '/order/getByUser?' + qs.stringify(data))
  return rst.data
}
// alipay
const alipay = async () => {
  const rst = await axios.post(goodsUrl + '/pay/alipay')
  console.log('pay', rst)
  window.localStorage.setItem('cacheAlipayHtml', rst.data)
  var newW = window.open('', '_blank')
  newW.document.write(window.localStorage.getItem('cacheAlipayHtml'))
  newW.document.close()
}
//  single_store_alipay
const singleStoreAlipay = async (data) => {
  const rst = await axios.post(payUrl + '/pay/single_store_alipay?' + qs.stringify(data))
  console.log(rst.data)
  window.localStorage.setItem('cacheAlipayHtml', rst.data)
  var newW = window.open('', '_blank')
  newW.document.write(window.localStorage.getItem('cacheAlipayHtml'))
  newW.document.close()
}
// 获取商品
const goodsList = async (data) => {
  const rst = await axios.get(goodsUrl + '/xr-goods/getList?' + qs.stringify(data))
  return rst.data
}
// 获取商品分类
const gt = async (data) => {
  const rst = await axios.get(goodsUrl + '/xr-goods-type/getList?' + qs.stringify(data))
  return rst.data
}
// 获取所有商家
const getAllStore = async () => {
  const data = await axios.get(userUrl + '/xr-store/allStore')
  return data.data
}
// 搜索商家
const searchStore = async (keyword) => {
  const data = await axios.post(userUrl + '/xr-store/search?keyword=' + keyword)
  return data.data
}
// 发送验证码
const sendCode = async (data) => {
  const rst = await axios.post(userUrl + '/xr-user/sendSms', qs.stringify(data), {
    headers: headers
  })
  return rst
}
// 登录
const login = async (data) => {
  const rst = await axios.post(userUrl + '/xr-user/login', qs.stringify(data), {
    headers: headers
  })
  return rst.data
}
export {
  getAllStore,
  sendCode,
  login,
  goodsList,
  gt,
  searchStore,
  alipay,
  singleStoreAlipay,
  getOrderFinished
}
